Recursion

1) Recursive method: 

A recursive is a method that calls itself. 
It consists of two main parts: 
a) Base case: input is manageable
b) Recursive step: the method calls itself but the parameter used would be closer to the base case

Example#1: Sum of integeres between 1 and n (Iterative + recursive) 

main ---> sumRec(0): n = 0 (no recursion)

main <-1-> sumRec(1): n = 1 : return 1 + sumRec(0) <-0-> sumRec(0): n = 0

main (sumRec(2)) --> sumRec(2): return 2 + sumRec(1) ---> sumRec(1): n = 1


private static long sumRec(int n) {
        if(n == 0) return 0;
        return n + sumRec(n - 1);
}

Recursive solution: time complexity: O(n) + space complexity: O(n) because of the recursive stack

Example#2: Factorial

Recursive solution: time complexity: O(n) + space complexity: O(n)

Example#3: Towers of Hanoi

Tower1		Tower2		Tower3

disk1
disk2
disk3

Move n disks from tower 1 to tower 3 using tower 2 as a temporary storage place

move n-1 disks from tower 1 to tower 2 via tower 3
move one disk from tower 1 to tower 3
move n-1 disks from tower 2 to tower 3 via tower 1









